﻿using System.Collections;
using System.Collections.Generic;

using UnityEngine;
using UnityEngine.UI;

/// <summary>
///   <para>Adds an outline to a graphic using IVertexModifier.</para>
/// </summary>
[AddComponentMenu("UGUI/Effects/Shadow")]
public class UGUIShadow : BaseMeshEffect
{
    private const float kMaxEffectDistance = 600f;

    [SerializeField]
    public Color effectColor = new Color(0f, 0f, 0f, 0.5f);

    [SerializeField]
    public Vector2 effectDistance = new Vector2(1f, -1f);

    [SerializeField]
    public bool useGraphicAlpha = true;

    /// <summary>
    ///   <para>Color for the effect.</para>
    /// </summary>
    public Color EffectColor
    {
        get
        {
            return this.effectColor;
        }
        set
        {
            this.effectColor = value;
            if (base.graphic != null)
            {
                base.graphic.SetVerticesDirty();
            }
        }
    }

    /// <summary>
    ///   <para>How far is the shadow from the graphic.</para>
    /// </summary>
    public Vector2 EffectDistance
    {
        get
        {
            return this.effectDistance;
        }
        set
        {
            if (value.x > 600f)
            {
                value.x = 600f;
            }
            if (value.x < -600f)
            {
                value.x = -600f;
            }
            if (value.y > 600f)
            {
                value.y = 600f;
            }
            if (value.y < -600f)
            {
                value.y = -600f;
            }

            if (this.effectDistance == value)
            {
                return;
            }

            this.effectDistance = value;

            if (base.graphic != null)
            {
                base.graphic.SetVerticesDirty();
            }
        }
    }

    /// <summary>
    ///   <para>Should the shadow inherit the alpha from the graphic?</para>
    /// </summary>
    public bool UseGraphicAlpha
    {
        get
        {
            return this.useGraphicAlpha;
        }
        set
        {
            this.useGraphicAlpha = value;
            if (base.graphic != null)
            {
                base.graphic.SetVerticesDirty();
            }
        }
    }

    protected UGUIShadow()
    {
    }

    //protected void OnValidate()
    //{
    //    this.EffectDistance = this.effectDistance;
    //    //base.OnValidate();
    //}

    protected void ApplyShadowZeroAlloc(List<UIVertex> verts, Color32 color, int start, int end, float x, float y)
    {
        int num = verts.Count + end - start;
        if (verts.Capacity < num)
        {
            verts.Capacity = num;
        }
        for (int i = start; i < end; i++)
        {
            UIVertex uIVertex = verts[i];
            verts.Add(uIVertex);

            Vector3 position = uIVertex.position;
            position.x += x;
            position.y += y;
            uIVertex.position = position;

            if (this.useGraphicAlpha)
            {
                Color32 color2 = color;
                color2.a = (byte)(color2.a * uIVertex.color.a / 255);
                uIVertex.color = color2;
            }
            else
            {
                uIVertex.color = color;
            }
            verts[i] = uIVertex;
        }
    }

    protected void ApplyShadow(List<UIVertex> verts, Color32 color, int start, int end, float x, float y)
    {
        this.ApplyShadowZeroAlloc(verts, color, start, end, x, y);
    }

    public override void ModifyMesh(VertexHelper vh)
    {
        if (!this.IsActive()) { return; }

        List<UIVertex> list = UGUIListPool<UIVertex>.Get();
        vh.GetUIVertexStream(list);

        this.ApplyShadow(list, this.EffectColor, 0, list.Count, this.effectDistance.x, this.effectDistance.y);
        vh.Clear();
        vh.AddUIVertexTriangleStream(list);

        UGUIListPool<UIVertex>.Release(list);
    }
}